package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.mapper.PurchaseOrderMapper;
import com.intelligent.mapper.PurchaseSupplierMapper;
import com.intelligent.mapper.SupplierReconciliationMapper;
import com.intelligent.mapper.TUserMapper;
import com.intelligent.pojo.PurchaseSupplier;
import com.intelligent.pojo.TUser;
import com.intelligent.service.PendingSupplierService;
import com.intelligent.vo.SupplierPendingPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PendingSupplierServiceImpl implements PendingSupplierService {

    @Autowired
    private PurchaseSupplierMapper supplierMapper;

    @Autowired
    private PurchaseOrderMapper orderMapper;

    @Autowired
    private SupplierReconciliationMapper supplierReconciliationMapper;

    @Autowired
    private TUserMapper tUserMapper;

    @Override
    public Page<PurchaseSupplier> pagePending(SupplierPendingPage req) {
        Page<PurchaseSupplier> page = new Page<>(req.getPageNum(), req.getPageSize());

        LambdaQueryWrapper<PurchaseSupplier> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PurchaseSupplier::getStatus, 0);
        if (req.getSupplierName()!=null && !req.getSupplierName().trim().isEmpty()){
            wrapper.like(PurchaseSupplier::getSupplierName, req.getSupplierName().trim());
        }
        supplierMapper.selectPage(page, wrapper);
        // 填充联系人姓名
        for (PurchaseSupplier s : page.getRecords()) {
            if (s.getUId() != null) {
                TUser user = tUserMapper.selectById(s.getUId());
                if (user != null) {
                    s.setContactPersonName(user.getRealName());
                }
            }
        }

        return page;
    }

    @Override
    public PurchaseSupplier detail(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public boolean createSupplier(PurchaseSupplier supplier) {
        if (supplier.getStatus()==null){
            supplier.setStatus(0);
        }
        return supplierMapper.insert(supplier) > 0;
    }
}


